package com.xintonghua.bussiness.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.AddListProductAdapter;
import com.xintonghua.bussiness.adapter.AddListProductTypeAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.AddProductTypeBean;
import com.xintonghua.bussiness.bean.AddProductListBean;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;

/**
 * 添加产品
 * Created by zhangxuemei on 2018/3/11.
 */

public class AddProductActivity extends BaseActivity {


    @BindView(R.id.lv_type)
    ListView lvType;
    @BindView(R.id.lv_product)
    ListView lvProduct;
    private AddListProductTypeAdapter mTypeAdapter;
    private AddListProductAdapter mProductAdapter;


    String type = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_add_product);
        ButterKnife.bind(this);

        type = JumpUtils.getString(this, "type");
        if (TextUtils.isEmpty(type)) {
            type = "";
        }

        initUI();
        requestTypeList();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        if (type.equals("1")) {
            setSimpleActionBar("产品价目表");
        } else {
            setSimpleActionBar("添加产品");
        }


        mTypeAdapter = new AddListProductTypeAdapter(this, new ArrayList<AddProductTypeBean>());
        lvType.setAdapter(mTypeAdapter);

        mProductAdapter = new AddListProductAdapter(this, new ArrayList<AddProductListBean>());
        lvProduct.setAdapter(mProductAdapter);
    }

    @OnItemClick(R.id.lv_type)
    public void onItemClickType(int position) {
        AddProductTypeBean bean = mTypeAdapter.getList().get(position);
        mTypeAdapter.setSelectItem(bean.getName());
        mTypeAdapter.notifyDataSetChanged();
        requestProductList(bean.getId());
    }

    @OnItemClick(R.id.lv_product)
    public void onItemClickProduct(int position) {
        if (!type.equals("1")) {
            AddProductListBean bean = mProductAdapter.getItem(position);
            mProductAdapter.notifyDataSetChanged();
            Bundle bundle = new Bundle();
            bundle.putSerializable(AddLogActivity.PRODUCT_KEY, bean);
            setResult(0, new Intent().putExtras(bundle));
            finish();
        }
    }

    private void requestTypeList() {
        HttpCent.getInstance(this).shopProductType(AuthManager.getId(), this, MyState.requestOne);
    }

    private void requestProductList(int projectId) {
        HttpCent.getInstance(this).shopProductList(projectId, this, MyState.requestTwo);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                List<AddProductTypeBean> list = JSONArray.parseArray(data.toString(), AddProductTypeBean.class);
                if (list != null && list.size() > 0) {
                    mTypeAdapter.setSelectItem(list.get(0).getName());
                    mTypeAdapter.setList(list);
                    mTypeAdapter.notifyDataSetChanged();
                    requestProductList(list.get(0).getId());
                } else {
                    mTypeAdapter.getList().clear();
                    mTypeAdapter.notifyDataSetChanged();
                }
                break;
            case MyState.requestTwo:
                List<AddProductListBean> list2 = JSONArray.parseArray(data.toString(), AddProductListBean.class);
                if (list2 != null && list2.size() > 0) {
                    mProductAdapter.setList(list2);
                    mProductAdapter.notifyDataSetChanged();
                } else {
                    mProductAdapter.getList().clear();
                    mProductAdapter.notifyDataSetChanged();
                }
                break;
        }
    }
}
